//import java.awt.Desktop;
//import java.io.BufferedReader;
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.InputStreamReader;
//import java.math.BigInteger;
//import java.net.URI;
//import java.net.URISyntaxException;
//import java.net.URL;
//import java.security.MessageDigest;
//import java.util.Properties;
//
//import javax.xml.parsers.ParserConfigurationException;
//
//import org.xml.sax.SAXException;
//
//import com.aetrion.flickr.Flickr;
//import com.aetrion.flickr.FlickrException;
//import com.aetrion.flickr.REST;
//import com.aetrion.flickr.RequestContext;
//import com.aetrion.flickr.auth.Auth;
//import com.aetrion.flickr.auth.AuthInterface;
//import com.aetrion.flickr.auth.Permission;
//import com.aetrion.flickr.util.IOUtilities;

import com.flickr4java.flickr.Flickr;
import com.flickr4java.flickr.FlickrException;
import com.flickr4java.flickr.REST;
import com.flickr4java.flickr.auth.Auth;
import com.flickr4java.flickr.auth.AuthInterface;
import com.flickr4java.flickr.auth.Permission;
import com.flickr4java.flickr.util.IOUtilities;

import org.scribe.model.Token;
import org.scribe.model.Verifier;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Scanner;

public class AuthExample {
//	Flickr f;
//    RequestContext requestContext;
//    String frob = "";
//    String token = "";
//    Properties properties = null;

   // public AuthExample() throws ParserConfigurationException, IOException, SAXException, URISyntaxException {
        //InputStream in = null;
        //try {
//            in = getClass().getResourceAsStream("/project.properties");
//            in = AuthExample.class.getResourceAsStream("/project.properties");
//            properties = new Properties();
//            properties.load(in);
        //} finally {
        //    IOUtilities.close(in);
       // }
//        f = new Flickr(
////            properties.getProperty("apiKey"),
////            properties.getProperty("secret"),
//        	"e918a0e8fa130b289c8e46e140bdf361",
//        	"a9fceafbbae8c31f",
//            new REST("www.flickr.com")
//        );
//        Flickr.debugStream = false;
//        requestContext = RequestContext.getRequestContext();
//        AuthInterface authInterface = f.getAuthInterface();
//        try {
//            frob = authInterface.getFrob();
//        } catch (FlickrException e) {
//            e.printStackTrace();
//        }
//        System.out.println("frob: " + frob);
//        URL url = authInterface.buildAuthenticationUrl(Permission.DELETE, frob);
//        String request = url.toExternalForm();
//        url.openConnection();
//        System.out.println(request);
//        URI uri = new URI(request);
//        Desktop desktop = null;
//        if (Desktop.isDesktopSupported()) 
//        {
//            desktop = Desktop.getDesktop();
//        }
//        if (desktop != null) 
//        {
//            desktop.browse(uri);   // Open Explorer to Confirm        
//        }
//        
//        
//        System.out.println("Press return after you granted access at this URL:");
//        System.out.println(url.toExternalForm());
//        BufferedReader infile =
//          new BufferedReader ( new InputStreamReader (System.in) );
//        String line = infile.readLine();
//        try {
//            Auth auth = authInterface.getToken(frob);
//            System.out.println("Authentication success");
//            // This token can be used until the user revokes it.
//            System.out.println("Token: " + auth.getToken());
//            System.out.println("nsid: " + auth.getUser().getId());
//            System.out.println("Realname: " + auth.getUser().getRealName());
//            System.out.println("Username: " + auth.getUser().getUsername());
//            System.out.println("Permission: " + auth.getPermission().getType());
//        } catch (FlickrException e) {
//            System.out.println("Authentication failed");
//            e.printStackTrace();
//        }
  //  }
	
	  public static void auth() throws IOException, FlickrException {
	        Properties properties;
	        InputStream in = null;
//	        try {
//	            in = AuthExample.class.getResourceAsStream("/setup.properties");
//	            properties = new Properties();
//	            properties.load(in);
//	        } finally {
//	            IOUtilities.close(in);
//	        }

	        Flickr flickr = new Flickr("e918a0e8fa130b289c8e46e140bdf361", "a9fceafbbae8c31f", new REST("www.flickr.com"));
	        Flickr.debugStream = false;
	        AuthInterface authInterface = flickr.getAuthInterface();

	        Scanner scanner = new Scanner(System.in);

	        Token token = authInterface.getRequestToken();
	        System.out.println("token: " + token);

	        String url = authInterface.getAuthorizationUrl(token, Permission.DELETE);
	        System.out.println("Follow this URL to authorise yourself on Flickr");
	        System.out.println(url);
	        System.out.println("Paste in the token it gives you:");
	        System.out.print(">>");

	        String tokenKey = scanner.nextLine();

	        Token requestToken = authInterface.getAccessToken(token, new Verifier(tokenKey));
	        System.out.println("Authentication success");

	        Auth auth = authInterface.checkToken(requestToken);

	        // This token can be used until the user revokes it.
	        System.out.println("Token: " + requestToken.getToken());
	        System.out.println("nsid: " + auth.getUser().getId());
	        System.out.println("Realname: " + auth.getUser().getRealName());
	        System.out.println("Username: " + auth.getUser().getUsername());
	        System.out.println("Permission: " + auth.getPermission().getType());
	    }

    public static void main(String[] args) {
    	try {
            AuthExample.auth();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.exit(0);
	}
}

